﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using System.Xml.Serialization;

namespace DiscoveryLogic.DataServices
{
    public class ModelFactory<T>
    {
        #region Public Static Methods

        public static T Load(string fileLocation)
        {
            if (!System.IO.Path.IsPathRooted(fileLocation))
            {
                string prefix = System.Web.HttpContext.Current.Server.MapPath("~");
                if (!fileLocation.StartsWith("\\"))
                {
                    if (!prefix.EndsWith(@"\"))
                    {
                        prefix += @"\";
                    }
                }
                fileLocation = prefix + fileLocation;
            }


            XmlSerializer serializer = new XmlSerializer(typeof(T));
            Stream stream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
            return Load(serializer, stream);
        }

        public static T Deserialize(string fileLocation)
        {
            if (!System.IO.Path.IsPathRooted(fileLocation))
            {
                string prefix = System.Web.HttpContext.Current.Server.MapPath("~");
                if (!fileLocation.StartsWith("\\"))
                {
                    if (!prefix.EndsWith(@"\"))
                    {
                        prefix += @"\";
                    }
                }
                fileLocation = prefix + fileLocation;
            }


            DataContractSerializer serializer = new DataContractSerializer(typeof(T));
            Stream stream = new FileStream(fileLocation, FileMode.Open, FileAccess.Read, FileShare.Read);
            T ret = (T)serializer.ReadObject(stream);
            stream.Close();

            return ret;
        }

        public static T Load(XmlSerializer serializer, Stream inputStream)
        {
            serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
            serializer.UnknownElement += new XmlElementEventHandler(serializer_UnknownElement);
            serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
            T ret = (T)serializer.Deserialize(inputStream);
            inputStream.Close();
            return ret;
        }

        static void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Unknown node " + e.Name + " at " + e.LineNumber + "," + e.LinePosition);


        }

        static void serializer_UnknownElement(object sender, XmlElementEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Unknown element " + e.Element.Name + " at " + e.LineNumber + "," + e.LinePosition);
        }

        static void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("Unknown attribute " + e.Attr.Name + " at " + e.LineNumber + "," + e.LinePosition);
        }

        #endregion Public Static Methods
    }
}
